package com.itic.market.mobile.uc.store;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.itic.appbase.framework.common.constants.DBConst;
import com.itic.appbase.framework.utils.StringHelper;
import com.itic.market.mobile.base.BaseMobileInterface;
import com.itic.market.system.marketuserproduct.persistence.model.MarketUserProduct;
import com.itic.market.system.marketuserproduct.persistence.model.MarketUserProductExample;
import com.itic.market.system.marketuserproduct.persistence.model.MarketUserProductExample.Criteria;
import com.itic.market.system.marketuserproduct.service.MarketUserProductService;
import com.itic.market.system.user.persistence.model.MarketUser;


/**
 * 用户设置接口类
 * @author 许小宝
 * 2017年07月26日  14时11分
 */
@Controller
@RequestMapping(value="/mobile/uc/store")
public class MarketUcStoreInterface
     extends BaseMobileInterface {
	
	@Autowired
	private MarketUserProductService marketUserProductService;
	
	
	/**
     * 获取当前店铺信息
     * @return 
     */
    @RequestMapping(value="/index")
    @ResponseBody
    public Map<String,Object> index( HttpServletRequest request, HttpServletResponse response, String token, String userId, String categoryId, String orderType,String minMaxPrice,String minMaxSales ){
    	Map<String, Object> resultMap = new HashMap<String,Object>();
    	String result = FAIL;
    	String errorCode = PARAM_ERROR_CODE;
    	String errorMsg = PARAM_ERROR_MSG;
    	try{
    		if( StringHelper.isNotBlank(token) ){
    			MarketUser user = getMarketUserByToken(token);
    			
    			//MarketUser user = getMarketUserByInvitingCode(storeCode);
    			if( null != user ){
    				
    				Map<String, Object> dataMap = new HashMap<String,Object>();
    				dataMap.put("user", user);
    				
    				MarketUserProductExample example = new MarketUserProductExample();
    				example = this.handlePageQuery(request, example);
    				if( StringHelper.isNotBlank(orderType) ){
    					if( "priceDesc".equals(orderType) ){
    	    				example.setOrderByClause("PRODUCT_PRICE DESC");
    	    			}
    	    			if( "priceAsc".equals(orderType) ){
    	    				example.setOrderByClause("PRODUCT_PRICE ASC");
    	    			}
    	    			if( "salesDesc".equals(orderType) ){
    	    				example.setOrderByClause("TOTAL_SALES DESC");
    	    			}
    				}else{
    					example.setOrderByClause("CREATE_TIME DESC");
    				}
    				
    				Criteria criteria = example.createCriteria();
    				criteria.andIsEffectiveEqualTo(DBConst.TRUE);
    				criteria.andUserIdEqualTo(user.getId());
    				
    				if( StringHelper.isNotBlank(categoryId) && !"0".equals(categoryId) ){
    					criteria.andCategoryIdEqualTo(categoryId);
    				}
    				if( StringHelper.isNotBlank(minMaxPrice) ){
    					String[] split = minMaxPrice.split("-");
    	        		if(split.length==2){
    	        			String minPrice = split[0];
    	            		String maxPrice = split[1];
    	            		if( StringHelper.isNotBlank(minPrice) ){
    	            			criteria.andProductPriceGreaterThanOrEqualTo(Double.parseDouble(minPrice));
    	            		}
    	            		if( StringHelper.isNotBlank(maxPrice) ){
    	            			criteria.andProductPriceLessThanOrEqualTo(Double.parseDouble(maxPrice));
    	            		}
    	        		}
    				}
    				if( StringHelper.isNotBlank(minMaxSales) ){
    					String[] split = minMaxSales.split("-");
    	        		if( split.length == 2 ){
    	        			String minSale = split[0];
    	            		String maxSale = split[1];
    	            		if( StringHelper.isNotBlank(minSale) ){
    	            			criteria.andTotalSalesGreaterThanOrEqualTo(Integer.parseInt(minSale));
    	            		}
    	            		if( StringHelper.isNotBlank(maxSale) ){
    	            			criteria.andTotalSalesLessThanOrEqualTo(Integer.parseInt(maxSale));
    	            		}
    	            		
    	        		}
    				}
    				int count = marketUserProductService.count(example);
    				List<MarketUserProduct> list = marketUserProductService.list(example);
    				
    				dataMap.put("products", list);
    				dataMap.put("totalCount", count + "");
    				//判断必要的参数
    				resultMap.put(DATA, dataMap);
    				result = SUCCESS;
    				errorCode = SUCCESS_ERROR_CODE;
    	        	errorMsg = SUCCESS_ERROR_MSG; 
    			}else{
    				result = LOGIN;
    				errorCode = LOGIN_ERROR_CODE;
    	        	errorMsg = LOGIN_ERROR_MSG; 
    			}
    			
    		}
    		
    		if( StringHelper.isNotBlank(userId) ){
				MarketUser user = marketUserService.get(userId);
				if( null != user ){
					Map<String, Object> dataMap = new HashMap<String,Object>();
    				dataMap.put("user", user);
    				
    				MarketUserProductExample example = new MarketUserProductExample();
    				example = this.handlePageQuery(request, example);
    				if( StringHelper.isNotBlank(orderType) ){
    					if( "priceDesc".equals(orderType) ){
    	    				example.setOrderByClause("PRODUCT_PRICE DESC");
    	    			}
    	    			if( "priceAsc".equals(orderType) ){
    	    				example.setOrderByClause("PRODUCT_PRICE ASC");
    	    			}
    	    			if( "salesDesc".equals(orderType) ){
    	    				example.setOrderByClause("TOTAL_SALES DESC");
    	    			}
    				}else{
    					example.setOrderByClause("CREATE_TIME DESC");
    				}
    				Criteria criteria = example.createCriteria();
    				criteria.andIsEffectiveEqualTo(DBConst.TRUE);
    				if( StringHelper.isNotBlank(categoryId) && !"0".equals(categoryId) ){
    					criteria.andCategoryIdEqualTo(categoryId);
    				}
    				if( StringHelper.isNotBlank(minMaxPrice) ){
    					String[] split = minMaxPrice.split("-");
    	        		if(split.length==2){
    	        			String minPrice = split[0];
    	            		String maxPrice = split[1];
    	            		if( StringHelper.isNotBlank(minPrice) ){
    	            			criteria.andProductPriceGreaterThanOrEqualTo(Double.parseDouble(minPrice));
    	            		}
    	            		if( StringHelper.isNotBlank(maxPrice) ){
    	            			criteria.andProductPriceLessThanOrEqualTo(Double.parseDouble(maxPrice));
    	            		}
    	        		}
    				}
    				if( StringHelper.isNotBlank(minMaxSales) ){
    					String[] split = minMaxSales.split("-");
    	        		if( split.length == 2 ){
    	        			String minSale = split[0];
    	            		String maxSale = split[1];
    	            		if( StringHelper.isNotBlank(minSale) ){
    	            			criteria.andTotalSalesGreaterThanOrEqualTo(Integer.parseInt(minSale));
    	            		}
    	            		if( StringHelper.isNotBlank(maxSale) ){
    	            			criteria.andTotalSalesLessThanOrEqualTo(Integer.parseInt(maxSale));
    	            		}
    	            		
    	        		}
    				}
    				int count = marketUserProductService.count(example);
    				List<MarketUserProduct> list = marketUserProductService.list(example);
    				
    				dataMap.put("products", list);
    				dataMap.put("totalCount", count + "");
    				//判断必要的参数
    				resultMap.put(DATA, dataMap);
    				result = SUCCESS;
    				errorCode = SUCCESS_ERROR_CODE;
    	        	errorMsg = SUCCESS_ERROR_MSG; 
				}else{
					result = FAIL;
					errorCode = STORE_CODE_ERROR_CODE;
    	        	errorMsg = STORE_CODE_ERROR_MSG; 
				}
			}
        	
    	}catch(Exception e){
    		errorCode = SERVER_ERROR_CODE;
        	errorMsg = SERVER_ERROR_MSG;
        	log(e);
    	}
    	resultMap.put(RESULT, result);
    	resultMap.put(ERROR_CODE, errorCode);
    	resultMap.put(ERROR_MSG, errorMsg);
    	isCrrossDomain(response);
    	return resultMap;
    } 
    
    
    /**
     * 用户添加推广的商品到自己店铺里来
     * @return 
     */
    @RequestMapping(value="/del")
    @ResponseBody
    public Map<String,Object> del( HttpServletRequest request, HttpServletResponse response, String token, String productId ){
    	Map<String, Object> resultMap = new HashMap<String,Object>();
    	String result = FAIL;
    	String errorCode = PARAM_ERROR_CODE;
    	String errorMsg = PARAM_ERROR_MSG;
    	try{
    		if( StringHelper.isNotBlank(token) && StringHelper.isNotBlank(productId) ){
    			MarketUser user = getMarketUserByToken(token);
    			if( null != user ){
    				 MarketUserProduct temp = new MarketUserProduct();
    				 temp.setId(productId);
    				 result = marketUserProductService.delete(temp);
                     result = SUCCESS;
                     errorCode = SUCCESS_ERROR_CODE;
                     errorMsg = SUCCESS_ERROR_MSG;
    			}else{
    				result = LOGIN;
    				errorCode = LOGIN_ERROR_CODE;
    	        	errorMsg = LOGIN_ERROR_MSG; 
    			}
    		}
        	
    	}catch(Exception e){
    		errorCode = SERVER_ERROR_CODE;
        	errorMsg = SERVER_ERROR_MSG;
        	log(e);
    	}
    	resultMap.put(RESULT, result);
    	resultMap.put(ERROR_CODE, errorCode);
    	resultMap.put(ERROR_MSG, errorMsg);
    	isCrrossDomain(response);
    	return resultMap;
    }  
    
    
    /**
     * 用户添加推广的商品到自己店铺里来
     * @return 
     */
    @RequestMapping(value="/share")
    @ResponseBody
    public Map<String,Object> share( HttpServletRequest request, HttpServletResponse response, String token ){
    	Map<String, Object> resultMap = new HashMap<String,Object>();
    	String result = FAIL;
    	String errorCode = PARAM_ERROR_CODE;
    	String errorMsg = PARAM_ERROR_MSG;
    	try{
    		if( StringHelper.isNotBlank(token) ){
    			MarketUser user = getMarketUserByToken(token);
    			if( null != user ){
    				 StoreVo vo = new StoreVo();
    				 vo.setName(user.getUserNickName());
    				 vo.setHeadImgUrl(user.getUserHeadImg());
    				 vo.setStoreUrl(getMarketStoreCommonUrl() + user.getId());
                     resultMap.put(DATA, vo );	
                     result = SUCCESS;
                     errorCode = SUCCESS_ERROR_CODE;
                     errorMsg = SUCCESS_ERROR_MSG;
    			}else{
    				result = LOGIN;
    				errorCode = LOGIN_ERROR_CODE;
    	        	errorMsg = LOGIN_ERROR_MSG; 
    			}
    		}
        	
    	}catch(Exception e){
    		errorCode = SERVER_ERROR_CODE;
        	errorMsg = SERVER_ERROR_MSG;
        	log(e);
    	}
    	resultMap.put(RESULT, result);
    	resultMap.put(ERROR_CODE, errorCode);
    	resultMap.put(ERROR_MSG, errorMsg);
    	isCrrossDomain(response);
    	return resultMap;
    }  
    
    
}